#! /usr/bin/env python

from mathex import fact


def f(n):
    return sum([fact(int(i)) for i in str(n) ])
    

s = 0
#for i in range(1,8):
#    for n in range(10**(i-1),min(10**i,362880*i+1)):
for n in range(3,362880*7):
    if f(n) == n:
        s += n
    if n%10000==0:
        print n

print s
